<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../admin/css/other/department.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deptName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="dept-table"></table>
    </div>
</div>

<script type="text/html" id="dept-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="dept-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="dept-status">
    <input type="checkbox" name="enable" value="{{d.deptId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="dept-enable" {{ d.status ? 'checked' : '' }}>
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup', 'easyAdmin'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;
        let easyAdmin = layui.easyAdmin;

        let MODULE_PATH = "dept/";
        window.render = function () {
            easyAdmin.httpGet('/sys/dept/data', function (result) {
                treetable.render({
                    data: result.data,
                    treeColIndex: 1,
                    treeSpid: 0,
                    treeIdName: 'deptId',
                    treePidName: 'pid',
                    skin: 'line',
                    method: 'get',
                    treeDefaultClose: true,
                    toolbar: '#dept-toolbar',
                    elem: '#table',
                    page: false,
                    defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'],
                    cols: [
                        [
                            {type: 'checkbox'},
                            {field: 'deptName', minWidth: 200, title: '部门名称'},
                            {field: 'address', title: '详细地址'},
                            {field: 'status', title: '状态', templet: '#dept-status'},
                            {field: 'sort', title: '排序', width: 100},
                            {title: '操作', templet: '#dept-bar', width: 120, align: 'center'}
                        ]
                    ]
                });
            })

        }
        render();

        form.on('submit(dept-query)', function (data) {
            var keyword = data.field.deptName;
            var $tds = $('#dept-table').next('.treeTable').find('.layui-table-body tbody tr td');
            if (!keyword) {
                $tds.css('background-color', 'transparent');
                layer.msg("请输入关键字", {icon: 5});
                return;
            }
            var searchCount = 0;
            $tds.each(function () {
                $(this).css('background-color', 'transparent');
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        $('body,html').stop(true);
                        $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            } else {
                treetable.expandAll('#dept-table');
            }
            return false;
        });

        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })

        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('switch(dept-enable)', function (obj) {
            easyAdmin.http({
                url: '/sys/dept',
                data: JSON.stringify({
                    deptId: this.value,
                    status: obj.elem.checked
                }),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg("操作成功", {icon: 1, time: 1000, area: ['100px', '65px']});
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000, area: ['260px', '65px']});
                    }
                }
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['450px', '500px'],
                content: MODULE_PATH + 'add.html'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                content: MODULE_PATH + 'edit.html?deptId=' + obj.data['deptId']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该部门', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                easyAdmin.http({
                    url: "/sys/dept/" + obj.data['deptId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            treetable.reload('#dept-table');
        }

        window.batchRemove = function (obj) {

            var checkIds = easyAdmin.checkField(obj, 'deptId');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                easyAdmin.http({
                    url: "/sys/dept/batch/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                area: ['260px', '80px'],
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>
